package com.scedu.controller;

import com.scedu.config.ProjectUrlConfig;
import com.scedu.constant.CookieConstant;
import com.scedu.constant.RedisConstant;
import com.scedu.domain.SellerInfo;
import com.scedu.enums.ResultEnums;
import com.scedu.service.SellerService;
import com.scedu.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/*
卖家用户操作
 */
@Slf4j
@Controller
@RequestMapping("/seller")
public class SellerUserController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ProjectUrlConfig projectUrlConfig;
    /*
    登录
     */
    @GetMapping("/login")
    private ModelAndView login(@RequestParam("openid") String openid,
                               HttpServletResponse response,
                               Map<String,Object> map){
        //1.openid和数据库进行数据匹配
        SellerInfo sellerInfo=sellerService.findSellerInfoByOpenid(openid);
        if(sellerInfo==null){
            //无记录
            log.error("登录失败,openid={}", openid);
            map.put("msg", ResultEnums.LOGIN_ERROR.getMsg());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        //2.设置token至reids
        String token= UUID.randomUUID().toString(); //javaJDK提供的一个自动生成主键的方法
        Integer expire= RedisConstant.EXPIRE;    //过期时间
        token=String.format(RedisConstant.TOKEN_PREFIX,token);//格式化TOKEN
        // redisTemplate.opsForValue().set("key","value",过期时间,时间格式);
        redisTemplate.opsForValue().set(token,openid,RedisConstant.EXPIRE, TimeUnit.SECONDS);
        //3.设置token至cookie
//        Cookie cookie=new Cookie("token",token);
//        cookie.setPath("/");
//        cookie.setMaxAge(RedisConstant.EXPIRE);
//        response.addCookie(cookie);
        CookieUtil.set(response, CookieConstant.cookiename,token,RedisConstant.EXPIRE);
        return  new ModelAndView("redirect:"+projectUrlConfig.getSell()+"/sell/seller/order/list");
    }
    /*
    注销
     */
    @GetMapping("/logout")
    private  ModelAndView logout( HttpServletRequest request,
                                  HttpServletResponse response,
                                  Map<String,Object> map){
     //1.从COOKIE中查询
       Cookie cookie= CookieUtil.get(request,CookieConstant.cookiename);
       if (cookie!=null){
           //2.从REIDS中清除
           redisTemplate.opsForValue().getOperations().delete(cookie.getValue());
           //3.清除cookie
           CookieUtil.set(response, CookieConstant.cookiename,null,0);
       }
        map.put("msg",ResultEnums.LOGOUT_SUCCESS.getMsg() );
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success", map);

    }
}
